package net.phoenix.repository.value;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.text.ParseException;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;

import net.phoenix.repository.RepositoryRuntimeException;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class DateValue extends BasicValue {

	public static SimpleDateFormat longFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat shortFormat = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat luceneFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

	private Calendar value;

	public DateValue(Calendar value) {
		this.value = value;
	}
	
	public DateValue(Date value) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(value);
		this.value = cal;
	}

	public DateValue(String strValue) throws ValueFormatException {
		if ((strValue == null) || (strValue.length() == 0)) {
			this.value = null;
			return;
		}
		this.value = Calendar.getInstance();
		try {

			this.value.setTime(longFormat.parse(strValue));
		} catch (ParseException ex) {
			try {
				this.value.setTime(shortFormat.parse(strValue));
			} catch (ParseException ex2) {
				throw new ValueFormatException(ex2);
			}
		}
	}

	public static Date toDate(String strValue) {
		if ((strValue == null) || (strValue.length() == 0))
			return null;
		try {
			return longFormat.parse(strValue);
		} catch (ParseException ex) {
			try {
				return (shortFormat.parse(strValue));
			} catch (ParseException ex2) {
				try {
					return (luceneFormat.parse(strValue));
				} catch (ParseException ex3) {
					try {
						return (dateFormat.parse(strValue));
					} catch (ParseException ex4) {
						throw new RepositoryRuntimeException(ex4);
					}
				}
			}
		}
	}

	public static String toString(Calendar value) {
		return longFormat.format(value);
	}

	@Override
	public int getType() {
		return PropertyType.DATE;
	}

	@Override
	public Calendar getDate() {
		return value;
	}

	@Override
	public String getString() {
		if (value != null)
			return longFormat.format(value.getTime());
		else
			return null;
	}

	@Override
	public long getLong() {
		if (this.value == null)
			return 0;
		else
			return value.getTimeInMillis();
	}

	@Override
	public double getDouble() {
		if (this.value == null)
			return 0;
		else
			return value.getTimeInMillis();
	}

	@Override
	public int compareTo(Value value) {
		try {
			return this.value.compareTo(value.getDate());
		} catch (RepositoryException e) {
			throw new RepositoryRuntimeException(e);
		}
	}

	@Override
	public boolean equals(Object o) {
		if (o == null)
			return false;
		if (o instanceof Value) {
			if (this.compareTo((Value) o) == 0)
				return true;
		} else if (o instanceof String) {
			Date date = toDate((String) o);
			return this.value.getTime().equals(date);
		} else if (o instanceof Date) {
			Date date = (Date) o;
			return this.value.getTime().equals(date);
		}
		return false;
	}
}